﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itegracion.Framework.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace Itegracion.Comunicaciones.Model
{
    public class Contacto
    {
        public static List<Element> TraerListado(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                SELECT
	                id, nombre, direccion, telefono
                FROM
	                Contacto
                ORDER BY
	                id ASC");
            DataTable dt = db.ExecuteDataSet(command).Tables[0];
            rpta.Add(new Element("rpta", dt));

            return rpta;
        }
        public static List<Element> Insertar(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                INSERT Contacto(nombre, direccion, telefono) VALUES(@nombre, @direccion, @telefono)");
            db.AddInParameter(command, "nombre", DbType.String, entrada[1].Value);
            db.AddInParameter(command, "direccion", DbType.String, entrada[2].Value);
            db.AddInParameter(command, "telefono", DbType.String, entrada[3].Value);
            db.ExecuteNonQuery(command);
            rpta.Add(new Element("rpta", "OK"));

            return rpta;
        }
        public static List<Element> Traer(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                SELECT id, nombre, direccion, telefono
                FROM Contacto
                WHERE id = @id");
            db.AddInParameter(command, "id", DbType.Int32, entrada[0].Value);
            DataTable dt = db.ExecuteDataSet(command).Tables[0];
            rpta.AddRange(dt.Rows.ToList());

            return rpta;
        }
        public static List<Element> Modificar(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                UPDATE Contacto 
                SET 
                    nombre = @nombre, 
                    direccion = @direccion, 
                    telefono = @telefono
                WHERE 
                    id = @id");
            db.AddInParameter(command, "id", DbType.String, entrada[0].Value);
            db.AddInParameter(command, "nombre", DbType.String, entrada[1].Value);
            db.AddInParameter(command, "direccion", DbType.String, entrada[2].Value);
            db.AddInParameter(command, "telefono", DbType.String, entrada[3].Value);
            db.ExecuteNonQuery(command);
            rpta.Add(new Element("rpta", "OK"));

            return rpta;
        }
        public static List<Element> Eliminar(List<Element> entrada)
        {
            List<Element> rpta = new List<Element>();

            Database db = DatabaseFactory.CreateDatabase("iAlmacen");
            DbCommand command = db.GetSqlStringCommand(@"
                DELETE FROM Contacto WHERE id = @id");
            db.AddInParameter(command, "id", DbType.Int32, entrada[0].Value);
            db.ExecuteNonQuery(command);
            rpta.Add(new Element("rpta", "OK"));

            return rpta;
        }
    }
}
